package enums;

import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * @className: ExecuteStateEnum
 * @author: ocxz1
 * @date: 2025/1/5 下午6:18
 * @Version: 1.0
 * @description: anneng-ltc-change-v1.0 执行状态枚举
 */
@Getter
@AllArgsConstructor
public enum ExecuteStateEnum {

    NULL("-001", "空状态"),
    NOT_STARTED("000", "未开始"),
    PENDING("001", "待执行"),
    EXECUTING("002", "正在执行"),
    SUCCESS("003", "执行成功"),
    FAILED("004", "执行失败"),
    CANCELLED("005", "已取消"),
    COMPLETED("006", "完成"),
    SKIP("007", "跳过执行"),

    ;
    public final String code;
    public final String description;

    /**
     * 根据状态码获取枚举实例
     */
    public static ExecuteStateEnum fromCode(String code) {
        for (ExecuteStateEnum state : values()) {
            if (state.code.equals(code)) {
                return state;
            }
        }
        throw new IllegalArgumentException("未知的状态码: " + code);
    }

    /**
     * 根据状态码获取枚举实例
     */
    public static ExecuteStateEnum fromName(String name) {
        for (ExecuteStateEnum state : values()) {
            if (state.name().equals(name)) {
                return state;
            }
        }
        throw new IllegalArgumentException("未知的状态码: " + name);
    }

    /**
     * 获取所有状态的描述信息
     */
    public static String getAllDescriptions() {
        return Arrays.stream(values())
                .map(state -> state.code + ": " + state.description)
                .collect(Collectors.joining(", "));
    }

    /**
     * 判断是否为终态（即不再发生变化的状态）
     */
    public boolean isTerminalState() {
        return this == COMPLETED || this == FAILED;
    }

    /**
     * 判断是否非终态
     * @return ture：非终态，false：终态
     */
    public boolean isNotTerminalState() {
        return !isTerminalState();
    }

    /**
     * 尝试从当前状态转换到新状态
     */
    public boolean canTransitionTo(ExecuteStateEnum newState) {
        if (this.isTerminalState()) {
            return false; // 终态不可再变
        }

        // 这里可以根据业务需求定义更复杂的转换规则
        return !this.equals(newState); // 简单示例：不能转换到相同状态
    }
}

    
    